package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author heixiaolv.com
 * @date 2021/12/5 16:56
 */

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService{


    @Autowired
    private OrderSettingDao orderSettingDao;

    //新增预约
    public void add(List<OrderSetting> list) {

        if (list != null && list.size() > 0){
            //遍历list集合
            for (OrderSetting orderSetting : list) {

                //判断数据库是否有预约值
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0){
                    //有，更新
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    //无，插入
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }


    public List<Map> getOrderSettingByMonth(String date) {

        String begin = date + "-1";
        String end = date + "-31";

        Map<String, String> map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);


        List<Map> result = new ArrayList<>();

        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        if (list != null && list.size() > 0){
            Map<String, Object> m = null;
            for (OrderSetting orderSetting : list) {
                m = new HashMap<>();
                m.put("date",orderSetting.getOrderDate().getDate());//获得日期（几号）
                m.put("number",orderSetting.getNumber());//可预约人数
                m.put("reservations", orderSetting.getReservations());//已预约人数

                result.add(m);
            }
        }

        return result;
    }

    //根据日期修改可预约人数
    public void editNumberByDate(OrderSetting orderSetting) {
        //判断数据库是否有预约值
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0){
            //当前日期已经进行了预约设置，需要进行修改操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            //无，插入
            orderSettingDao.add(orderSetting);
        }
    }

}
